package com.han.hotelplat.interceptor;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;






import com.alibaba.fastjson.JSONObject;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.comm.utils.RequestUtil;

public class ExportInterceptor extends HandlerInterceptorAdapter {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// 请求处理完成之后
		System.out.println("请求处理完成啦！");
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		// 处理器执行完毕之后
		System.out.println("处理器执行完毕啦！");
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse res, Object handler) throws Exception {
		// 处理器实际执行之前
		System.out.println(String.format("%s : 处理器执行之前！url:%s", DateUtil.format(new Date(), DateUtil.YMDHMS), request.getRequestURI()));
		String url = request.getRequestURL().toString();
		if(url.contains("exp")){
			/*Boolean re = (Boolean) RequestUtil.getSessionAttribute("isExport");
			if(re== null || !re){
				res.setContentType("text/html;charset=GBK");
				res.getWriter().write("没有权限");
				res.getWriter().flush();
				return false;
			}*/
			
		}
		return super.preHandle(request, res, handler);
	}
	
	/**
	 *  使用	response 输出JSON
	 * @param hresponse
	 * @param resultMap
	 * @throws IOException
	 */
	public static void out(ServletResponse response, Map<String, Object> resultMap) {
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");// 设置编码
			response.setContentType("application/json");// 设置返回类型
			out = response.getWriter();
			out.println(JSONObject.toJSONString(resultMap).toString());// 输出
		} catch (Exception e) {
		} finally {
			if (null != out) {
				out.flush();
				out.close();
			}
		}
	}

}
